package org.dromara.school.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.school.domain.vo.TeacherVo;
import org.dromara.school.listener.StudentImportExcelListener;
import org.dromara.school.listener.TeacherImportExcelListener;
import org.dromara.school.service.ICampusService;
import org.dromara.school.service.IClassesService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.school.domain.vo.StudentVo;
import org.dromara.school.domain.bo.StudentBo;
import org.dromara.school.service.IStudentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生管理
 *
 * @author gjb
 * @date 2025-05-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/school/student")
public class StudentController extends BaseController {

    private final IStudentService studentService;

    private final IClassesService classesService;

    private final ICampusService campusService;

    /**
     * 查询学生管理列表
     */
    @SaCheckPermission("school:student:list")
    @GetMapping("/list")
    public TableDataInfo<StudentVo> list(StudentBo bo, PageQuery pageQuery) {
        return studentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出学生管理列表
     */
    @SaCheckPermission("school:student:export")
    @Log(title = "学生管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StudentBo bo, HttpServletResponse response) {
        List<StudentVo> list = studentService.queryList(bo);
        ExcelUtil.exportExcel(list, "学生管理", StudentVo.class, response);
    }

    /**
     * 获取学生管理详细信息
     *
     * @param studentId 主键
     */
    @SaCheckPermission("school:student:query")
    @GetMapping("/{studentId}")
    public R<StudentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long studentId) {
        return R.ok(studentService.queryById(studentId));
    }

    /**
     * 新增学生管理
     */
    @SaCheckPermission("school:student:add")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StudentBo bo) {
        return toAjax(studentService.insertByBo(bo));
    }

    /**
     * 修改学生管理
     */
    @SaCheckPermission("school:student:edit")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StudentBo bo) {
        return toAjax(studentService.updateByBo(bo));
    }

    /**
     * 删除学生管理
     *
     * @param studentIds 主键串
     */
    @SaCheckPermission("school:student:remove")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] studentIds) {
        return toAjax(studentService.deleteWithValidByIds(List.of(studentIds), true));
    }


    @SaCheckPermission("school:student:query")
    @GetMapping(value = "getStudentParents/{id}")
    public R<StudentVo> getParentsInfo(@PathVariable("id") Long id)
    {
        return R.ok(studentService.selectStudentParentsById(id));
    }



    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "学生信息导入模版", StudentVo.class, response);
    }





    @PostMapping("/upload")
    public R upload(@RequestPart("file") MultipartFile file) {

        try {
            EasyExcel.read(file.getInputStream(), StudentVo.class,new StudentImportExcelListener(studentService,campusService,classesService)).sheet().doRead();
            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.fail("文件导入失败！");
    }



    /**
     * 添加家长信息
     */
    @SaCheckPermission("school:student:add")
    @Log(title = "学生管理家长绑定", businessType = BusinessType.INSERT)
    @PostMapping("/addparents")
    public R addStudentParent(@RequestBody StudentBo studentBo)
    {
        int i = this.studentService.editStudentParent(studentBo);
        return R.ok();
    }





}
